/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.bookingmanager.adapters.impl;

import cz.muni.fi.pa165.bookingmanager.adapters.RoomAdapter;
import cz.muni.fi.pa165.bookingmanager.dto.HotelDTO;
import cz.muni.fi.pa165.bookingmanager.dto.RoomDTO;
import cz.muni.fi.pa165.bookingmanager.entities.Hotel;
import cz.muni.fi.pa165.bookingmanager.entities.Room;
import cz.muni.fi.pa165.bookingmanager.exceptions.IllegalOrphanException;
import cz.muni.fi.pa165.bookingmanager.exceptions.NonexistentEntityException;
import cz.muni.fi.pa165.bookingmanager.exceptions.PreexistingEntityException;
import cz.muni.fi.pa165.bookingmanager.services.RoomService;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author Jiří Holuša
 */
@Stateless
public class RoomAdapterImpl implements RoomAdapter {

    @EJB
    private RoomService roomService;

    @Override
    public void createRoom(RoomDTO room) throws IllegalOrphanException, PreexistingEntityException {
        Room roomEntity = AdaptersHelper.roomDTOToEntity(room);

        roomService.createRoom(roomEntity);
    }

    @Override
    public void updateRoom(RoomDTO room) throws IllegalOrphanException, NonexistentEntityException {
        Room roomEntity = AdaptersHelper.roomDTOToEntity(room);

        roomService.updateRoom(roomEntity);
    }

    @Override
    public void deleteRoom(RoomDTO room) throws IllegalOrphanException, NonexistentEntityException {
        Room roomEntity = AdaptersHelper.roomDTOToEntity(room);

        roomService.deleteRoom(roomEntity);
    }

    @Override
    public RoomDTO getRoomById(Long id) {
        Room room = roomService.getRoomById(id);

        RoomDTO roomDTO = AdaptersHelper.roomEntityToDTO(room);
        return roomDTO;
    }

    @Override
    public RoomDTO findRoomByNumber(String number, HotelDTO hotel) {
        Hotel hotelEntity = AdaptersHelper.hotelDTOToEntity(hotel);

        Room room = roomService.findRoomByNumber(number, hotelEntity);

        if (room != null) {
            RoomDTO roomDTO = AdaptersHelper.roomEntityToDTO(room);
            return roomDTO;
        } else {
            return null;
        }
    }

    @Override
    public List<RoomDTO> findAllHotelRooms(HotelDTO hotel) {
        Hotel hotelEntity = AdaptersHelper.hotelDTOToEntity(hotel);

        List<Room> list = roomService.findAllHotelRooms(hotelEntity);

        List<RoomDTO> roomsDTO = new ArrayList<RoomDTO>();
        for (Room roomEntity : list) {
            roomsDTO.add(AdaptersHelper.roomEntityToDTO(roomEntity));
        }
        return roomsDTO;
    }

    @Override
    public List<RoomDTO> findRoomsByPrice(BigDecimal from, BigDecimal to) {


        List<Room> list = roomService.findRoomsByPrice(from, to);

        List<RoomDTO> roomsDTO = new ArrayList<RoomDTO>();
        for (Room roomEntity : list) {
            roomsDTO.add(AdaptersHelper.roomEntityToDTO(roomEntity));
        }
        return roomsDTO;
    }

    @Override
    public List<RoomDTO> findRoomsByPrice(BigDecimal from, BigDecimal to, HotelDTO hotel) {
        Hotel hotelEntity = AdaptersHelper.hotelDTOToEntity(hotel);


        List<Room> list = roomService.findRoomsByPrice(from, to, hotelEntity);

        List<RoomDTO> roomsDTO = new ArrayList<RoomDTO>();
        for (Room roomEntity : list) {
            roomsDTO.add(AdaptersHelper.roomEntityToDTO(roomEntity));
        }
        return roomsDTO;
    }

    @Override
    public List<RoomDTO> findRoomsByCapacity(int capacity) {
        List<Room> list = roomService.findRoomsByCapacity(capacity);

        List<RoomDTO> roomsDTO = new ArrayList<RoomDTO>();
        for (Room roomEntity : list) {
            roomsDTO.add(AdaptersHelper.roomEntityToDTO(roomEntity));
        }
        return roomsDTO;
    }

    @Override
    public List<RoomDTO> findRoomsByCapacity(int capacity, HotelDTO hotel) {
        Hotel hotelEntity = AdaptersHelper.hotelDTOToEntity(hotel);

        List<Room> list = roomService.findRoomsByCapacity(capacity, hotelEntity);

        List<RoomDTO> roomsDTO = new ArrayList<RoomDTO>();
        for (Room roomEntity : list) {
            roomsDTO.add(AdaptersHelper.roomEntityToDTO(roomEntity));
        }
        return roomsDTO;
    }

    public void setRoomService(RoomService roomService) {
        this.roomService = roomService;
    }
}
